import { themes } from './model'
import { LocalStorageService } from '@/utils/storage.ts'
/** 修改页面中的样式变量值 */
const changeStyle = (obj) => {
  for (let key in obj) {
    document.getElementsByTagName('body')[0].style.setProperty(`--${key}`, obj[key])
  }
}
/** 改变主题的方法 */
export const setTheme = (themeName, custom = {}) => {
  // 保存主题到本地，下次进入使用该主题
  LocalStorageService.set('theme', themeName)
  const themeConfig = themes[themeName]
  // 如果有主题名称，那么则采用我们定义的主题
  if (themeConfig) {
    // localStorage.setItem('primary', themeConfig.primary) // 保存主题色到本地
    LocalStorageService.set('themeObj', themes[themeName])
    changeStyle(themeConfig) // 改变样式
  } else {
    let themeConfig = Object.assign(themes['default'], custom)
    LocalStorageService.set('themeObj', themeConfig)
    changeStyle(themeConfig)
  }
}

/** 设置默认主题 */
export function setDefaultTheme() {
  const defaultTheme = LocalStorageService.get('themeObj')
  if (defaultTheme) {
    changeStyle(defaultTheme)
  } else {
    changeStyle(themes['default'])
  }
}
